package com.quiz.bis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quiz.bis.domain.bo.PaperStrategyBO;
import com.quiz.bis.domain.bo.PaperTemplateBO;
import com.quiz.bis.domain.model.PaperStrategy;
import com.quiz.bis.domain.model.PaperTemplate;
import com.quiz.bis.domain.vo.PaperStrategyVO;
import com.quiz.bis.domain.vo.PaperTemplateVO;
import com.quiz.bis.mapper.PaperStrategyMapper;
import com.quiz.bis.mapper.PaperTemplateMapper;
import com.quiz.bis.service.IPaperStrategyService;
import com.quiz.bis.service.IPaperTemplateService;
import com.quiz.common.core.domain.R;
import com.quiz.common.core.domain.model.LoginUser;
import com.quiz.common.exception.ServiceException;
import com.quiz.common.mybatis.core.page.PageQuery;
import com.quiz.common.mybatis.core.page.TableDataInfo;
import com.quiz.common.utils.SecurityUtils;
import com.quiz.common.utils.StringUtils;
import com.quiz.common.utils.bean.BeanUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 试卷模板服务实现
 *
 * @author ahuan
 */
@Service
@RequiredArgsConstructor
public class PaperTemplateServiceImpl implements IPaperTemplateService {

    private final PaperTemplateMapper templateMapper;

    @Override
    public TableDataInfo<PaperTemplateVO> pageTemplatePage(PageQuery pageQuery, String name) {
        Page<PaperTemplate> page = templateMapper.selectPage(pageQuery.build(),
                Wrappers.<PaperTemplate>lambdaQuery()
                        .like(StringUtils.isNotBlank(name), PaperTemplate::getName, name)
                        .eq(PaperTemplate::getDelFlag, 0));
        List<PaperTemplateVO> pageVo = BeanUtil.copyToList(page.getRecords(), PaperTemplateVO.class);
        TableDataInfo<PaperTemplateVO> build = TableDataInfo.build(pageVo);
        build.setTotal(page.getTotal());
        return build;
    }

    @Override
    public List<PaperTemplateVO> pageTemplateList(String name) {
        List<PaperTemplate> list = templateMapper.selectList(
                Wrappers.<PaperTemplate>lambdaQuery()
                        .like(StringUtils.isNotBlank(name), PaperTemplate::getName, name)
                        .eq(PaperTemplate::getDelFlag, 0));
        return BeanUtil.copyToList(list, PaperTemplateVO.class);
    }

    @Override
    @Transactional
    public R<Long> createTemplate(PaperTemplateBO templateBO) {
        // 验证模板名称
        if (StringUtils.isBlank(templateBO.getName())) {
            return R.fail("模板名称不能为空");
        }

        // 检查名称是否已存在
        if (templateMapper.exists(Wrappers.<PaperTemplate>lambdaQuery()
                .eq(PaperTemplate::getName, templateBO.getName())
                .eq(PaperTemplate::getDelFlag, 0))) {
            return R.fail("模板名称已存在");
        }

        // 创建模板
        PaperTemplate template = BeanUtil.copyProperties(templateBO, PaperTemplate.class);
        template.setCreateByName(SecurityUtils.getUsername());

        // 生成配置
        JSONObject config = new JSONObject();
        config.set("types", templateBO.getQuestionTypeConfig())
//                .set("passRate", null)
//                .set("timePerQuestion", null)
                .set("knowledgePoints", templateBO.getKnowledgeIds());

        template.setConfigJson(config.toString());
        templateMapper.insert(template);
        return R.ok(template.getId());
    }

    @Override
    public R<PaperTemplateVO> getTemplateDetail(Long templateId) {
        PaperTemplate template = templateMapper.selectById(templateId);
        if (template == null || template.getDelFlag() == 1) {
            return R.fail("模板不存在或已删除");
        }

        return R.ok(BeanUtil.copyProperties(template, PaperTemplateVO.class));
    }

    @Override
    @Transactional
    public R<Void> updateTemplate(PaperTemplateBO templateBO) {
        if (templateBO.getId() == null) {
            return R.fail("模板ID不能为空");
        }

        PaperTemplate template = templateMapper.selectById(templateBO.getId());
        if (template == null || template.getDelFlag() == 1) {
            return R.fail("模板不存在或已删除");
        }

        // 更新模板信息
        BeanUtils.copyProperties(templateBO, template);
        templateMapper.updateById(template);
        return R.ok();
    }

    @Override
    @Transactional
    public R<Void> deleteTemplate(Long templateId) {
        PaperTemplate template = templateMapper.selectById(templateId);
        if (template == null || template.getDelFlag() == 1) {
            return R.fail("模板不存在或已删除");
        }

        // 逻辑删除
        template.setDelFlag(1);
        templateMapper.updateById(template);
        return R.ok();
    }
}